package com.lanswon.qzsmk.util.pdfutil;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.lanswon.qzsmk.exception.SystemException;

/**
 * PDF文件处理
 * @author 惠国磊
 *
 */
public class PDFApplicationRegTicket {
		
	/*1.模板文件的路径*/
    private String templatePdfPath;  
    private String ttcPath;
    /*3.目标文件的路径*/
    private String targetPdfpath;  
    private ApplicationRegisterTicket appticket;  
    //20180619 图片路径
    private String imagePath;
    //设置二维码图片的路径
    private String QRCodeImgPath;
    //设置个人图片坐标
    private final static float x = (float)448.5;
    private final static float y = (float)555.5;
    //设置二维码坐标
    private final static float qrx = (float)469;
    private final static float qry = (float)772;
    
    private final static Logger logger = (Logger) LoggerFactory.getLogger(PDFApplicationRegTicket.class);
    
    public PDFApplicationRegTicket() {  
        super();  
     }

	
    
    public PDFApplicationRegTicket(String templatePdfPath, String ttcPath, String targetPdfpath,
			ApplicationRegisterTicket ticket,String imagePath,String QRCodeImgPath) {
		this.templatePdfPath = templatePdfPath;
		this.ttcPath = ttcPath;
		this.targetPdfpath = targetPdfpath;
		this.appticket = ticket;
		this.imagePath = imagePath;
		this.QRCodeImgPath = QRCodeImgPath;
	}



	public void tempAppReg(File file){
		logger.info("tempAppReg########start()");
		PdfReader reader;
		try {
			reader = new PdfReader(templatePdfPath);
		} catch (IOException e) {
			e.printStackTrace();
			String msg="系统创建PDF异常";
			logger.error(msg);
			throw new SystemException(SystemException.IOException, msg);
			
		}  
        ByteArrayOutputStream bos = new ByteArrayOutputStream();  
        PdfStamper ps;
		try {
			ps = new PdfStamper(reader, bos);
		} catch (DocumentException e) {
			e.printStackTrace();
			String msg="系统创建PDF模板异常";
			logger.error(msg);
			throw new SystemException(SystemException.DocumentException, msg);
		} catch (IOException e) {
			e.printStackTrace();
			String msg="系统创建PDF模板异常";
			logger.error(msg);
			throw new SystemException(SystemException.IOException, msg);
		} 
        
        /*使用中文字体 */  
        BaseFont bf = null;
		try {
			bf = BaseFont.createFont(PDFApplicationRegTicket.class.getResource("/") +
				       "simsun.ttc,1",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
		} catch (DocumentException e)
		{
			e.printStackTrace();
			String msg="系统创建字体异常";
			logger.error(msg);
			throw new SystemException(SystemException.DocumentException, msg);

		} catch (IOException e) {
			e.printStackTrace();
			String msg="系统创建字体异常";
			logger.error(msg);
			throw new SystemException(SystemException.IOException, msg);

		} 
        ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();   
        fontList.add(bf);
        
        AcroFields af = ps.getAcroFields();  
        af.setSubstitutionFonts(fontList);
        
        // 文本域添加          
        if(logger.isDebugEnabled()){
        	logger.debug("采集网点====>"+appticket.getCollectionpoint());
        	logger.debug("采集单编号====>"+appticket.getCollectionNo());
        	logger.debug("柜员编号====>"+appticket.getEmployeeID());
        	logger.debug("卡产品====>"+appticket.getCardCode());
        	logger.debug("卡类型====>"+appticket.getCardTypeCode());
        	logger.debug("姓名====>"+appticket.getCustomerName());
        	logger.debug("性别====>"+appticket.getGender());
        	logger.debug("民族====>"+appticket.getNationality());
        	logger.debug("出生日期====>"+appticket.getBirth());
        	logger.debug("证件类型====>"+appticket.getPaperType());
        	logger.debug("证件号码====>"+appticket.getPaperNo());
        	logger.debug("户籍地省份====>"+appticket.getPrAddrProvince());
        	logger.debug("户籍地城市====>"+appticket.getPrAddrCity());
        	
        	logger.debug("现居住地址省份====>"+appticket.getCurAddrProvince());
        	logger.debug("现居住地址城市====>"+appticket.getCurAddrCity());
        	logger.debug("现居住地址区====>"+appticket.getCurAddrCounty());
        	logger.debug("现居住地址街道====>"+appticket.getCurAddrStreet());
        	logger.debug("现居住地址详细====>"+appticket.getCurAddrDetailInfo());
        	
        	logger.debug("通讯地址省份====>"+appticket.getContactAddrProvince());
        	logger.debug("通讯地址城市====>"+appticket.getContactAddrCity());
        	logger.debug("通讯地址区====>"+appticket.getContactAddrCounty());
        	logger.debug("通讯地址街道====>"+appticket.getContactAddrStreet());
        	logger.debug("通讯地址详细====>"+appticket.getContactAddrDetailedInfo());
        	logger.debug("手机====>"+appticket.getMobile());
        	logger.debug("固定电话====>"+appticket.getHomePhone());
        	logger.debug("邮箱====>"+appticket.getEmail());
        	logger.debug("邮编====>"+appticket.getContactPostalcode());
        }
        try {
			af.setField("collectionpoint",appticket.getCollectionpoint());
		
        af.setField("collectionNo",appticket.getCollectionNo());
        af.setField("employeeID",appticket.getEmployeeID());
        af.setField("cardCode",appticket.getCardCode());
        af.setField("cardTypeCode",appticket.getCardTypeCode());
        af.setField("customerName", appticket.getCustomerName());
        af.setField("nationality", appticket.getNationality());
        af.setField("paperNo", appticket.getPaperNo());
        af.setField("gender",appticket.getGender());
        af.setField("birth",appticket.getBirth());
        af.setField("paperType",appticket.getPaperType());
        af.setField("prAddrProvince",appticket.getPrAddrProvince());
        af.setField("prAddrCity",appticket.getPrAddrCity());
        
        af.setField("curAddrProvince",appticket.getCurAddrProvince());
        af.setField("curAddrCity",appticket.getCurAddrCity());
        af.setField("curAddrCounty",appticket.getCurAddrCounty());
        af.setField("curAddrStreet",appticket.getCurAddrStreet());
        af.setField("curAddrDetailedInfo",appticket.getCurAddrDetailInfo());
              
        af.setField("contactAddrProvince",appticket.getContactAddrProvince());
        af.setField("contactAddrCity",appticket.getContactAddrCity());
        af.setField("contactAddrCounty",appticket.getContactAddrCounty());
        af.setField("contactAddrStreet",appticket.getContactAddrStreet());
        af.setField("contactAddrDetailedInfo",appticket.getContactAddrDetailedInfo());
        af.setField("homePhone", appticket.getHomePhone());
        af.setField("mobile",appticket.getMobile());
        af.setField("email",appticket.getEmail());
        af.setField("contactPostalcode",appticket.getContactPostalcode());
        af.setField("date",appticket.getDate());
        
        } catch (IOException e) {
        	e.printStackTrace();
        	String msg="PDF设置字段异常";
			logger.error(msg);
			throw new SystemException(SystemException.IOException, msg);
			
		} catch (DocumentException e) {
			e.printStackTrace();
			String msg="PDF设置字段异常";
			logger.error(msg);
			throw new SystemException(SystemException.DocumentException, msg);
			
		}
        PdfContentByte over = ps.getOverContent(1);
        //添加图片
        Image image = null;
		try {
			image = Image.getInstance(imagePath);
		} catch (BadElementException e) {
			e.printStackTrace();
			String msg="图片实例化异常";
			logger.error(msg);
			throw new SystemException(SystemException.BadElementException, msg);
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
			String msg="图片实例化异常";
			logger.error(msg);
			throw new SystemException(SystemException.MalformedURLException, msg);
			
		} catch (IOException e) {
			e.printStackTrace();
			String msg="图片实例化异常";
			logger.error(msg);
			throw new SystemException(SystemException.IOException, msg);
			
		}
        image.setAbsolutePosition(x,y);
        try {
			over.addImage(image);
		} catch (DocumentException e) {
			e.printStackTrace();
			String msg="PDF添加图片异常";
			logger.error(msg);
			throw new SystemException(SystemException.DocumentException, msg);
			
		}
        
        
        
        //添加二维码
       /* Image image1 =null;
		try {
			image1 = Image.getInstance(QRCodeImgPath);
		} catch (BadElementException e) {
			e.printStackTrace();
			String msg="图片实例化异常";
			logger.error(msg);
			throw new SystemException(SystemException.BadElementException, msg);
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
			String msg="图片实例化异常";
			logger.error(msg);
			throw new SystemException(SystemException.MalformedURLException, msg);
			
		} catch (IOException e) {
			e.printStackTrace();
			String msg="图片实例化异常";
			logger.error(msg);
			throw new SystemException(SystemException.IOException, msg);
			
		}
        image1.setAbsolutePosition(qrx,qry);
        image1.scaleToFit(72,80);
        try {
			over.addImage(image1);
		} catch (DocumentException e) {
			e.printStackTrace();
			String msg="PDF添加图片异常";
			logger.error(msg);
			throw new SystemException(SystemException.DocumentException, msg);
		}*/
        
        
        ps.setFormFlattening(true);  
        try {
			ps.close();
		} catch (DocumentException e) {
			e.printStackTrace();
			String msg="PDF模板关闭异常";
			logger.error(msg);
			throw new SystemException(SystemException.DocumentException, msg);
		} catch (IOException e) {
			e.printStackTrace();
			String msg="PDF模板关闭异常";
			logger.error(msg);
			throw new SystemException(SystemException.IOException, msg);
		}
        
        FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			String msg="系统找不到文件异常";
			logger.error(msg);
			throw new SystemException(SystemException.FileNotFoundException, msg);

		}  
        try {
			fos.write(bos.toByteArray());
			fos.flush();
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
			String msg="IO流异常";
			logger.error(msg);
			throw new SystemException(SystemException.IOException, msg);

		}
        
        logger.info("tempAppReg########end()");
	}



	public String getTemplatePdfPath() {
		return templatePdfPath;
	}



	public void setTemplatePdfPath(String templatePdfPath) {
		this.templatePdfPath = templatePdfPath;
	}



	public String getTtcPath() {
		return ttcPath;
	}



	public void setTtcPath(String ttcPath) {
		this.ttcPath = ttcPath;
	}



	public String getTargetPdfpath() {
		return targetPdfpath;
	}



	public void setTargetPdfpath(String targetPdfpath) {
		this.targetPdfpath = targetPdfpath;
	}



	public ApplicationRegisterTicket getAppticket() {
		return appticket;
	}



	public void setAppticket(ApplicationRegisterTicket appticket) {
		this.appticket = appticket;
	}



	public String getImagePath() {
		return imagePath;
	}



	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}



	public String getQRCodeImgPath() {
		return QRCodeImgPath;
	}

	public void setQRCodeImgPath(String qRCodeImgPath) {
		QRCodeImgPath = qRCodeImgPath;
	}
	
	
}
